﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
	public class Tab_RawMaterialDal
	{
		IDBHelp db = DBHelp.Create();
		 Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
		StringBuilder strSql = new StringBuilder();

		#region 是否存在该记录
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		/// <param name="RawMaterialID">标识列ID</param>
		public bool Exists(int RawMaterialID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select count(1) from [Tab_RawMaterial] where RawMaterialID=@rawmaterialid ");
			htSyn.Add("@rawmaterialid", RawMaterialID);
			return db.IsExists(strSql.ToString(), htSyn);
		}
		#endregion

		#region 得到该表数据列表
		/// <summary>
		/// 得到该表数据列表
		/// </summary>
		public IList<Tab_RawMaterial> GetModel()
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("Select [RawMaterialID],[RawMaterialClassID],[RawMaterialTitle],[RawMaterialDescription],[RawMaterialKey],[RawMaterialName],[RawMaterialImgUrl],[RawMaterialInfo] FROM [Tab_RawMaterial]");
			 return getTab_RawMaterialBySql(strSql.ToString());
		}
		#endregion

        #region 得到该表数据列表
        /// <summary>
        /// 得到该表数据列表
        /// </summary>
        public IList<Tab_RawMaterial> GetModelByFID()
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("Select [RawMaterialID],[RawMaterialClassID],[RawMaterialTitle],[RawMaterialDescription],[RawMaterialKey],[RawMaterialName],[RawMaterialImgUrl],[RawMaterialInfo] FROM [Tab_RawMaterial]");
            return getTab_RawMaterialBySql(strSql.ToString());
        }
        #endregion

		#region 根据标识列查询实体
		/// <summary>
		/// 根据标识列查询实体
		/// </summary>
		/// <param name="RawMaterialID">标识列ID</param>
		/// <returns></returns>
		public Tab_RawMaterial GetModel(int rawmaterialid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select [RawMaterialID],[RawMaterialClassID],[RawMaterialTitle],[RawMaterialDescription],[RawMaterialKey],[RawMaterialName],[RawMaterialImgUrl],[RawMaterialInfo] FROM [Tab_RawMaterial]");
			strSql.Append(" where RawMaterialID=@rawmaterialid ");
			htSyn.Add("@rawmaterialid", rawmaterialid);
			DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
			Tab_RawMaterial tab_rawmaterialModel = new Tab_RawMaterial();
			if (dt.Rows.Count > 0)
			{
				tab_rawmaterialModel.rawMaterialClassID=Convert.ToInt32(dt.Rows[0]["RawMaterialClassID"]);
				tab_rawmaterialModel.rawMaterialTitle=dt.Rows[0]["RawMaterialTitle"].ToString();
				tab_rawmaterialModel.rawMaterialDescription=dt.Rows[0]["RawMaterialDescription"].ToString();
				tab_rawmaterialModel.rawMaterialKey=dt.Rows[0]["RawMaterialKey"].ToString();
				tab_rawmaterialModel.rawMaterialName=dt.Rows[0]["RawMaterialName"].ToString();
				tab_rawmaterialModel.rawMaterialImgUrl=dt.Rows[0]["RawMaterialImgUrl"].ToString();
				tab_rawmaterialModel.rawMaterialInfo=dt.Rows[0]["RawMaterialInfo"].ToString();
			}
			return tab_rawmaterialModel;
		}
		#endregion

		#region 修改信息
		/// <summary>
		/// 修改信息
		/// </summary>
		/// <param name="Tab_RawMaterial">实体</param>
		/// <returns></returns>
		public bool Update(Tab_RawMaterial tab_rawmaterialModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("update [Tab_RawMaterial] set ");
			strSql.Append("[RawMaterialClassID]=@rawmaterialclassid,");
			strSql.Append("[RawMaterialTitle]=@rawmaterialtitle,");
			strSql.Append("[RawMaterialDescription]=@rawmaterialdescription,");
			strSql.Append("[RawMaterialKey]=@rawmaterialkey,");
			strSql.Append("[RawMaterialName]=@rawmaterialname,");
			strSql.Append("[RawMaterialImgUrl]=@rawmaterialimgurl,");
			strSql.Append("[RawMaterialInfo]=@rawmaterialinfo");

			strSql.Append(" where RawMaterialID=@rawmaterialid ");
			htSyn.Add("@rawmaterialid", tab_rawmaterialModel.rawMaterialID);
			htSyn.Add("@rawmaterialclassid", tab_rawmaterialModel.rawMaterialClassID);
			htSyn.Add("@rawmaterialtitle", tab_rawmaterialModel.rawMaterialTitle);
			htSyn.Add("@rawmaterialdescription", tab_rawmaterialModel.rawMaterialDescription);
			htSyn.Add("@rawmaterialkey", tab_rawmaterialModel.rawMaterialKey);
			htSyn.Add("@rawmaterialname", tab_rawmaterialModel.rawMaterialName);
			htSyn.Add("@rawmaterialimgurl", tab_rawmaterialModel.rawMaterialImgUrl);
			htSyn.Add("@rawmaterialinfo", tab_rawmaterialModel.rawMaterialInfo);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 删除信息
		/// <summary>
		/// 删除信息
		/// </summary>
		/// <param name="RawMaterialID">标识列ID</param>
		/// <returns></returns>
		public bool Delete(int RawMaterialID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("delete from [Tab_RawMaterial] where RawMaterialID=@rawmaterialid");
			htSyn.Add("rawmaterialid", RawMaterialID);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 添加信息
		/// <summary>
		/// 添加信息
		/// </summary>
		/// <param name="Tab_RawMaterial">实体</param>
		/// <returns></returns>
		public bool Insert(Tab_RawMaterial tab_rawmaterialModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("insert into [Tab_RawMaterial]([RawMaterialClassID],[RawMaterialTitle],[RawMaterialDescription],[RawMaterialKey],[RawMaterialName],[RawMaterialImgUrl],[RawMaterialInfo]) values (@rawmaterialclassid,@rawmaterialtitle,@rawmaterialdescription,@rawmaterialkey,@rawmaterialname,@rawmaterialimgurl,@rawmaterialinfo)");
			htSyn.Add("@rawmaterialclassid", tab_rawmaterialModel.rawMaterialClassID);
			htSyn.Add("@rawmaterialtitle", tab_rawmaterialModel.rawMaterialTitle);
			htSyn.Add("@rawmaterialdescription", tab_rawmaterialModel.rawMaterialDescription);
			htSyn.Add("@rawmaterialkey", tab_rawmaterialModel.rawMaterialKey);
			htSyn.Add("@rawmaterialname", tab_rawmaterialModel.rawMaterialName);
			htSyn.Add("@rawmaterialimgurl", tab_rawmaterialModel.rawMaterialImgUrl);
			htSyn.Add("@rawmaterialinfo", tab_rawmaterialModel.rawMaterialInfo);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 根据SQL语句获取集合
		/// <summary>
		///根据SQL语句获取集合
		/// </summary>
		/// <param name="strSql">sql语句</param>
		public IList<Tab_RawMaterial> getTab_RawMaterialBySql(string strSql)
		{
			IList<Tab_RawMaterial> list = new List<Tab_RawMaterial>();
			DataTable dt = db.GetDataTable(strSql);
			if (dt.Rows.Count > 0)
			{
				foreach (DataRow dr in dt.Rows)
				{
					Tab_RawMaterial tab_rawmaterialModel = new Tab_RawMaterial();
					tab_rawmaterialModel.rawMaterialID= Convert.ToInt32(dr["RawMaterialID"]);
					tab_rawmaterialModel.rawMaterialClassID= Convert.ToInt32(dr["RawMaterialClassID"]);
					tab_rawmaterialModel.rawMaterialTitle=dr["RawMaterialTitle"].ToString();
					tab_rawmaterialModel.rawMaterialDescription=dr["RawMaterialDescription"].ToString();
					tab_rawmaterialModel.rawMaterialKey=dr["RawMaterialKey"].ToString();
					tab_rawmaterialModel.rawMaterialName=dr["RawMaterialName"].ToString();
					tab_rawmaterialModel.rawMaterialImgUrl=dr["RawMaterialImgUrl"].ToString();
					tab_rawmaterialModel.rawMaterialInfo=dr["RawMaterialInfo"].ToString();
					list.Add(tab_rawmaterialModel);
				}
			}
			return list;
		}
		#endregion

        #region 材料分页
        /// <summary>
        /// 材料分页
        /// </summary>
        /// <param name="pageIndex">索引页</param>
        /// <param name="pageSize">页数</param>
        /// <param name="rawMaterialClassID">材料类别ID</param>
        /// <returns></returns>
        public DataTable pageForRawMaterial(int pageIndex,int pageSize,int rawMaterialClassID) 
        {
            return db.GetMPageDataTable(pageIndex, pageSize, "[RawMaterialID],[RawMaterialClassID],[RawMaterialTitle],[RawMaterialDescription],[RawMaterialKey],[RawMaterialName],[RawMaterialImgUrl],[RawMaterialInfo]", "[Tab_RawMaterial]", " and [RawMaterialClassID]=" + rawMaterialClassID, "[RawMaterialID]", false);
        }
        #endregion

        #region 材料分页
        /// <summary>
        /// 材料分页
        /// </summary>
        /// <param name="pageIndex">索引页</param>
        /// <param name="pageSize">页数</param> 
        /// <returns></returns>
        public DataTable pageForRawMaterial(int pageIndex, int pageSize)
        {
            return db.GetMPageDataTable(pageIndex, pageSize, "[RawMaterialID],[RawMaterialClassID],[RawMaterialTitle],[RawMaterialDescription],[RawMaterialKey],[RawMaterialName],[RawMaterialImgUrl],[RawMaterialInfo]", "[Tab_RawMaterial]", "", "[RawMaterialID]", false);
        }
        #endregion

        #region 获得材料数
        /// <summary>
        /// 获得材料数
        /// </summary>
        /// <returns></returns>
        public int getRecordCount() 
        {
            object objRecordCount = null;
            int i = 0;
            objRecordCount = db.ExecuteScalar("select count(1) from [Tab_RawMaterial]");
            if (objRecordCount != null)
            {
                i = Convert.ToInt32(objRecordCount);
            }
            return i;
        }
        #endregion

        #region 获得材料数
        /// <summary>
        /// 获得产品数
        /// </summary>
        /// <param name="rawMaterialClassID">材料类别ID</param>
        /// <returns></returns>
        public int getRecordCount(int rawMaterialClassID)
        {
            object objRecordCount = null;
            int i = 0;
            strSql.Append("select count(1) from [Tab_RawMaterial] where [RawMaterialClassID]=@RawMaterialClassID");
            htSyn.Clear();
            htSyn.Add("@RawMaterialClassID", rawMaterialClassID);
            objRecordCount = db.ExecuteScalar(strSql.ToString(), htSyn);
            if (objRecordCount != null)
            {
                i = Convert.ToInt32(objRecordCount);
            }
            return i;
        }
        #endregion
    }
}
